import {Injectable} from '@angular/core';
import {CacheKey} from '../../shared/components/dhlcomponent/enums';
import {CacheDataService} from './cache.data.service';

@Injectable()
export class PermissionService {

  constructor(private cacheService: CacheDataService) {
  }

  checkPermission(permission): boolean {
    return this.isHasPermission(null, permission);
  }

  isHasPermission(menuCode, permission): boolean {

    const loginUser = this.cacheService.getItem(CacheKey.LOGIN_USER);
    if (loginUser.isSuperManager) {
      return true;
    }
    const projectActions = this.cacheService.getItem(CacheKey.ACTIONS);
    permission = menuCode ? menuCode + '_' + permission : permission;
    if (projectActions && projectActions.length > 0) {
      for (const {code} of projectActions) {
        if (code == permission) {
          return true;
        }
      }
    }
    return false;
  }
}

